<template>
  <div class="profile">
    <div class="container">
      <div class="profile-header">
        <h1>个人资料</h1>
        <p>管理您的账户信息和设置</p>
      </div>
      
      <div class="profile-content">
        <div class="profile-card">
          <div class="card-header">
            <h3>基本信息</h3>
          </div>
          <div class="profile-info">
            <div class="info-item">
              <label>用户ID</label>
              <div class="info-value">{{ userInfo?.user_id }}</div>
            </div>
            <div class="info-item">
              <label>用户名</label>
              <div class="info-value">{{ userInfo?.username }}</div>
            </div>
            <div class="info-item">
              <label>账户状态</label>
              <div class="status-badge success">正常</div>
            </div>
          </div>
        </div>
        
        <div class="profile-card">
          <div class="card-header">
            <h3>安全设置</h3>
          </div>
          <div class="security-info">
            <div class="security-item">
              <div class="security-label">
                <h4>访问令牌</h4>
                <p>用于API访问的短期令牌</p>
              </div>
              <div class="security-actions">
                <button @click="refreshToken" class="btn btn-primary" :disabled="tokenLoading">
                  {{ tokenLoading ? '刷新中...' : '刷新令牌' }}
                </button>
              </div>
            </div>
            
            <div class="security-item">
              <div class="security-label">
                <h4>刷新令牌</h4>
                <p>用于获取新的访问令牌</p>
              </div>
              <div class="security-actions">
                <span class="status-text">已配置</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="profile-card">
          <div class="card-header">
            <h3>账户操作</h3>
          </div>
          <div class="account-actions">
            <button @click="refreshUserInfo" class="btn btn-primary" :disabled="loading">
              {{ loading ? '刷新中...' : '刷新用户信息' }}
            </button>
            <button @click="handleLogout" class="btn btn-danger" :disabled="logoutLoading">
              {{ logoutLoading ? '退出中...' : '退出登录' }}
            </button>
          </div>
        </div>
      </div>
      
      <div v-if="message" :class="['alert', messageType]">
        {{ message }}
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

export default {
  name: 'Profile',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const loading = ref(false)
    const tokenLoading = ref(false)
    const logoutLoading = ref(false)
    const message = ref('')
    const messageType = ref('')
    
    const userInfo = computed(() => authStore.userInfo)
    
    const showMessage = (msg, type = 'info') => {
      message.value = msg
      messageType.value = type
      setTimeout(() => {
        message.value = ''
      }, 5000)
    }
    
    const refreshUserInfo = async () => {
      loading.value = true
      try {
        const result = await authStore.fetchUserInfo()
        if (result.success) {
          showMessage('用户信息刷新成功！', 'success')
        } else {
          showMessage(result.message, 'error')
        }
      } catch (error) {
        showMessage('刷新失败: ' + error.message, 'error')
      } finally {
        loading.value = false
      }
    }
    
    const refreshToken = async () => {
      tokenLoading.value = true
      try {
        const result = await authStore.refreshAccessToken()
        if (result.success) {
          showMessage('令牌刷新成功！', 'success')
        }
      } catch (error) {
        showMessage('令牌刷新失败: ' + error.message, 'error')
      } finally {
        tokenLoading.value = false
      }
    }
    
    const handleLogout = async () => {
      logoutLoading.value = true
      try {
        await authStore.logout()
        showMessage('已退出登录', 'success')
        router.push('/login')
      } catch (error) {
        showMessage('退出失败: ' + error.message, 'error')
      } finally {
        logoutLoading.value = false
      }
    }
    
    return {
      userInfo,
      loading,
      tokenLoading,
      logoutLoading,
      message,
      messageType,
      refreshUserInfo,
      refreshToken,
      handleLogout
    }
  }
}
</script>

<style scoped>
.profile {
  padding: 40px 0;
  min-height: calc(100vh - 80px);
}

.profile-header {
  text-align: center;
  margin-bottom: 40px;
}

.profile-header h1 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
}

.profile-header p {
  font-size: 18px;
  color: #666;
}

.profile-content {
  max-width: 800px;
  margin: 0 auto;
}

.profile-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  overflow: hidden;
}

.profile-info {
  padding: 20px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item label {
  font-weight: 500;
  color: #666;
  min-width: 100px;
}

.info-value {
  color: #333;
  font-weight: 600;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.success {
  background: #d4edda;
  color: #155724;
}

.security-info {
  padding: 20px;
}

.security-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
}

.security-item:last-child {
  border-bottom: none;
}

.security-label h4 {
  margin: 0 0 5px 0;
  color: #333;
  font-size: 16px;
}

.security-label p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.security-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-text {
  color: #28a745;
  font-weight: 500;
  font-size: 14px;
}

.account-actions {
  padding: 20px;
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

@media (max-width: 768px) {
  .profile {
    padding: 20px 0;
  }
  
  .profile-header h1 {
    font-size: 24px;
  }
  
  .profile-header p {
    font-size: 16px;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  
  .security-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .account-actions {
    flex-direction: column;
  }
}
</style>
